﻿using BCCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCEntity.TMS.Contraband.ContrabandCategory;
using System.Data;

namespace BCData.TMS.Contraband.ContrabandCategory
{
    public class ContrabandCategoryData : IContrabandCategoryData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		                                ContrabandCategoryId,
		                                ContrabandCategory,
		                                CreateAdminId,
		                                CreateAdminName,
		                                CreateTime,
		                                UpdateTime,
		                                EditAdminId,
		                                EditAdminName 
                                    FROM
                                        ContrabandCategory";



        #region 构造函数
        public ContrabandCategoryData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新违禁品分类纪录
        /// </summary>
        /// <param name="entity">违禁品分类信息</param>
        /// <returns></returns>
        public async Task<ContrabandCategoryEntity> InsertAsync(ContrabandCategoryEntity entity)
        {
            return await this.database.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改违禁品分类记录
        /// </summary>
        /// <param name="entity">违禁品分类信息</param>
        /// <returns></returns>
        public async Task<ContrabandCategoryEntity> UpdateAsync(ContrabandCategoryEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除违禁品分类记录
        /// </summary>
        /// <param name="contrabandCategoryId">违禁品分类Id</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int contrabandCategoryId)
        {
            return await this.database.Master.ExecuteAsync("DELETE FROM ContrabandCategory WHERE ContrabandCategoryId = ?ContrabandCategoryId", new { contrabandCategoryId });
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取违禁品分类记录
        /// </summary>
        /// <param name="contrabandCategoryId">违禁品分类Id</param>
        /// <returns></returns>
        public async Task<ContrabandCategoryEntity> GetEntityAsync(int contrabandCategoryId)
        {
            return await this.database.Slave.QueryEntityAsync<ContrabandCategoryEntity>($"{QUERY} WHERE ContrabandCategoryId = ?ContrabandCategoryId", new { contrabandCategoryId });
        }
        #endregion

        public async Task<ContrabandCategoryEntity> GetEntityByContrabandCategory(string contrabandCategory)
        {
            return await database.Slave.QueryEntityAsync<ContrabandCategoryEntity>($"{QUERY} WHERE ContrabandCategory = ?ContrabandCategory", new { contrabandCategory });
        }

        public async Task<IEnumerable<ContrabandCategoryEntity>> GetList(string contrabandCategory)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(contrabandCategory))
            {
                wheres.Add("ContrabandCategory LIKE ?ContrabandCategory");
                parameters.Add("?ContrabandCategory", $"%{contrabandCategory}%", DbType.AnsiString);
            }
            string sql = string.Format("{0} {1}", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await database.Slave.QueryAsync<ContrabandCategoryEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<ContrabandCategoryEntity>, int)> Query(string contrabandCategory, DateTime? createStartDate, DateTime? createEndDate, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(contrabandCategory))
            {
                wheres.Add("ContrabandCategory LIKE ?ContrabandCategory");
                parameters.Add("?ContrabandCategory", $"%{contrabandCategory}%", DbType.AnsiString);
            }
            if (createStartDate.HasValue)
            {
                wheres.Add("CreateTime >= ?CreateStartDate");
                parameters.Add("?CreateStartDate", createStartDate.Value.Date, DbType.Date);
            }
            if (createEndDate.HasValue)
            {
                wheres.Add("CreateTime < ?CreateEndDate");
                parameters.Add("?CreateEndDate", createEndDate.Value.AddDays(1).Date, DbType.Date);
            }
            string sql = string.Format(@"{0} {1} ORDER BY ContrabandCategoryId DESC LIMIT {2},{3};
                                SELECT COUNT(1) FROM ContrabandCategory {1}",
                                QUERY,
                                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                                (pageIndex - 1) * pageSize,
                                pageSize);
            return await database.Slave.QueryPageableListAsync<ContrabandCategoryEntity>(sql, parameters);
        }
    }
}
